package com.axend.lib_base.adapter

import androidx.appcompat.widget.AppCompatTextView
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LiveData
import com.axend.lib_base.R
import com.axend.lib_base.ext.roundToOneDecimalPlace
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_base.view.input.CustomInputView

object CustomViewBindingAdapter {

    @JvmStatic
    @BindingAdapter("showInput")
    fun setShowInput(view: CustomInputView, showInputLiveData: LiveData<Boolean>) {
        showInputLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(true, false, false,false)
            }
        }
    }


    @JvmStatic
    @BindingAdapter("showSpinner")
    fun setShowSpinner(view: CustomInputView, showSpinnerLiveData: LiveData<Boolean>) {
        showSpinnerLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, true, false,false)
            }
        }
    }


    /**
     * 定义app:showText
     */
    @JvmStatic
    @BindingAdapter("showText")
    fun setShowText(view: CustomInputView, showTextLiveData: LiveData<Boolean>) {
        showTextLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, false, true,false)
            }
        }
    }

    /**
     * 定义app:showSwitch
     */
    @JvmStatic
    @BindingAdapter("showSwitch")
    fun setShowSwitch(view: CustomInputView, showSwitchLiveData: LiveData<Boolean>) {
        showSwitchLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, false, false,true)
            }
        }
    }

    /**
     * 定义float转string带默认值--
     */
    @JvmStatic
    @BindingAdapter("floatToString")
    fun setFloatToString(view: AppCompatTextView, value: Float) {
        view.text = value.roundToOneDecimalPlace()
    }

    /**
     * 定义获取温度单位
     */
    @JvmStatic
    @BindingAdapter("temperatureText")
    fun setTemperature(view: AppCompatTextView, index: Int) {
        view.text = CommUtils.getStringArray(R.array.app_temp_units)[index]
    }
}